package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.service.PositionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统岗位表，实际是部门和职务的对应关系表 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.POS_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/position")
public class PositionController extends SysBaseController {
	@Resource
	PositionService positionService;

	/**
	 * 根据用户id查询岗位列表
	 * @return ResultData
	 */
	@Action(description = "根据用户id或组织id查询岗位列表",
			detail = "根据<#if StringUtil.isNotEmpty(userId)><#assign entity=sysUserService.getOneById(userId)/>用户【${entity.fullName}】" +
					"<#elseif StringUtil.isNotEmpty(orgId)><#assign entity=sysOrgService.getSysOrgById(orgId)/>组织【${entity.orgName}】" +
					"<#else><#assign entity=sysOrgService.getSysOrgById(currOrgId)/>组织【${entity.orgName}】</#if>查询岗位列表<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@GetMapping("/queryOrgPos")
	public ResultData queryOrgPos(HttpServletRequest request) {
			String userId = RequestUtil.getString(request,"userId");
			String orgId = RequestUtil.getString(request,"orgId");
			SysAuditThreadLocalHolder.putParamerter("userId", userId);
			SysAuditThreadLocalHolder.putParamerter("orgId", orgId);
			SysAuditThreadLocalHolder.putParamerter("currOrgId", ContextUtil.getCurrentOrgId());
		try {
			List list = this.positionService.queryOrgPos(userId,orgId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success", 0, list);
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}
}